//
//  KColumnBaseLogic.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//

#import "KColumnBaseLogic.h"
#import "NSObject+KObserverHelper.h"

@interface KColumnInfo (Private)

- (void)containerOffsetChanged;

@end

@interface KColumnHorizontalView (Private)

- (void)refreshViewSize;

- (void)containerOffsetChanged;

@end

@interface KColumnBaseLogic()

@property (weak,nonatomic) NSObject<KColumnListControl> * control;

@property (strong,nonatomic) NSArray<KColumnInfo *> * columnList;

@property (weak,nonatomic) __kindof UIScrollView * observedContainerView;

@property (nonatomic) BOOL vertical;

@end

@implementation KColumnBaseLogic
@synthesize horizontalView = _horizontalView;

- (instancetype)initWithControl:(NSObject<KColumnListControl> *)control
{
    if (self = [super init])
    {
        _control = control;
    }
    return self;
}

- (NSArray<KColumnInfo *> *)columnList
{
    if (self.vertical)
    {
        return _columnList;
    }
    return _horizontalView.columnList;
}

- (KColumnHorizontalView *)horizontalView
{
    if (_horizontalView == nil)
    {
        [self prepareHorizontalView];
    }
    return _horizontalView;
}

- (CGSize)viewSize
{
    if (self.control.containerView == nil || self.control.containerView.frame.size.height == 0)
    {
        return [UIScreen mainScreen].bounds.size;
    }
    return self.control.containerView.frame.size;
}

- (void)prepareHorizontalView
{
    CGSize viewSize = [self viewSize];
    if (_horizontalView == nil)
    {
        _horizontalView = [[KColumnHorizontalView alloc] initWithFrame:CGRectMake(0, 0, viewSize.width, viewSize.height)];
    }
    else if (!CGSizeEqualToSize(viewSize, _horizontalView.frame.size))
    {
        CGRect frame = _horizontalView.frame;
        frame.size = viewSize;
        _horizontalView.frame = frame;
    }
    _horizontalView.delegate = self.delegate;
    _horizontalView.control = self.control;
}

- (void)reloadHorizontalColumns:(NSArray<KColumnInfo *> *)columnList headerView:(UIView *)headerView toIndex:(NSInteger)toIndex
{
    if (self.control.containerView != nil)
     {
         [self prepareHorizontalView];
         [columnList makeObjectsPerformSelector:@selector(setView:) withObject:self.horizontalView];
         [_horizontalView reloadWithColumnList:columnList headerView:headerView toIndex:toIndex];
         self.vertical = NO;
     }
}

- (void)reloadVerticalColumns:(NSArray<KColumnInfo *> *)columnList
{
    if (self.control.containerView != nil)
    {
        [self prepareHorizontalView];
        if (_horizontalView.columnList.count > 0)
        {
            _horizontalView.control = nil;
            [_horizontalView reloadWithColumnList:nil headerView:nil toIndex:0];
        }
        for (NSInteger index = 0; index < columnList.count; index++)
        {
            KColumnInfo * column = columnList[index];
            CGRect frame = column.view.frame;
            frame.size = [self viewSize];
            column.view.frame = frame;
            if (column.spec == nil)
            {
                column.spec = [NSString stringWithFormat:@"%ld",index];
            }
        }
        _columnList = [NSArray arrayWithArray:columnList];
        [self.control loadColumnList:_columnList];
        self.vertical = YES;
    }
}

- (void)reload
{
    if (self.vertical)
    {
        [self reloadVerticalColumns:_columnList];
    }
    else
    {
        [self.horizontalView reload:self.horizontalView.index];
    }
}

- (void)startKVO:(UIScrollView *)containerView
{
    if (self.observedContainerView != containerView)
    {
        [self stopKVO];
        [containerView k_addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
        self.observedContainerView = containerView;
    }
}

- (void)stopKVO
{
    if (self.observedContainerView != nil)
    {
        [self.observedContainerView k_removeObserver:self forKeyPath:@"contentOffset" context:nil];
        self.observedContainerView = nil;
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if (self.vertical)
    {
        [_columnList makeObjectsPerformSelector:@selector(containerOffsetChanged)];
    }
    else
    {
        [_horizontalView containerOffsetChanged];
    }
}

@end
